<template>
  <div class="afterManage-combination-page app-container">
    <el-form ref="dateForm" :inline="true" label-width="80px" :model="dateForm">
      <el-form-item label="分组名称" prop="groupName">
        <el-input v-model="dateForm.groupName" clearable placeholder="请输入分组名称" style="width: 260px;"></el-input>
      </el-form-item>
      <el-form-item style="margin-left: 20px">
        <el-button class="bg-green" @click="getList">查询</el-button>
        <el-button v-if="!isOpen" type="danger" @click="addOrUpdateHandle()">添加</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :data="list"
      border fit
      size="mini"
      @row-click="submitHandle"
      :header-cell-style="{background:'#dcedff',color:'#32639d'}"
      style="width: 100%;"
    >
      <el-table-column type="index" label="序号" align="center" width="60" fixed></el-table-column>
      <el-table-column prop="groupName" label="分组名称" min-width="130" fixed show-overflow-tooltip>
        <template slot-scope="{row}">
          <span v-if="row.hasNetWorth" style="margin-left: 10px;cursor: pointer;color: #1890ff;" @click.stop="groupDeltailHandle(row.groupId)">{{ row.groupName }}</span>
          <span v-else style="margin-left: 10px;cursor: pointer;color: #666;" @click.stop="$message('该投资分组净值不存在，无法查看详情')">{{ row.groupName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="investManageVo.latestNetWorth" label="单位净值" min-width="130" show-overflow-tooltip align="center"></el-table-column>
      <el-table-column prop="investManageVo.investMoney" label="投资金额(万元)" width="130" align="center"></el-table-column>
      <el-table-column prop="investManageVo.investIncom" label="收益(元)" min-width="140" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.investManageVo.investIncom||row.investManageVo.investIncom!==null" :class="rmoney(row.investManageVo.investIncom)>=0?'positiveFc':'negativeFc'">{{ row.investManageVo.investIncom }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="investManageVo.investIncomeRate" label="收益率" min-width="120" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.investManageVo.investIncomeRate||row.investManageVo.investIncomeRate!==null" :class="row.investManageVo.investIncomeRate>=0?'positiveFc':'negativeFc'">{{ row.investManageVo.investIncomeRate }}%</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="investManageVo.todayIncomRate" label="当日收益率" min-width="120" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.investManageVo.todayIncomRate||row.investManageVo.todayIncomRate!==null" :class="row.investManageVo.todayIncomRate>=0?'positiveFc':'negativeFc'">{{ row.investManageVo.todayIncomRate }}%</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="investEmail" label="近1月收益率" min-width="120" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.investManageVo.investNetWorthExtends&&row.investManageVo.investNetWorthExtends[1].incomeRate" 
          :class="row.investManageVo.investNetWorthExtends[1].incomeRate>=0?'positiveFc':'negativeFc'">{{ row.investManageVo.investNetWorthExtends[1].incomeRate }}%</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="investEmail" label="近6月收益率" min-width="120" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.investManageVo.investNetWorthExtends&&row.investManageVo.investNetWorthExtends[3].incomeRate" 
          :class="row.investManageVo.investNetWorthExtends[3].incomeRate>=0?'positiveFc':'negativeFc'">{{ row.investManageVo.investNetWorthExtends[3].incomeRate }}%</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="investEmail" label="近1年收益率" min-width="120" show-overflow-tooltip align="center">
        <template slot-scope="{row}">
          <span v-if="row.investManageVo.investNetWorthExtends&&row.investManageVo.investNetWorthExtends[4].incomeRate" 
          :class="row.investManageVo.investNetWorthExtends[4].incomeRate>=0?'positiveFc':'negativeFc'">{{ row.investManageVo.investNetWorthExtends[4].incomeRate }}%</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="investManageVo.latestNetWorthTime" label="净值日期" width="100" align="center">
        <template slot-scope="{row}">
          <span>{{ row.investManageVo.latestNetWorthTime | parseTime }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createdBy" label="创建人" min-width="100" show-overflow-tooltip align="center"></el-table-column>
      <el-table-column v-if="!isOpen" label="操作" align="center" width="270">
        <template slot-scope="{row}">
          <el-button class="filter-item" size="mini" type="primary" icon="el-icon-connection" @click="goAfterInvestHandle(row)">
            下属投资
          </el-button>
          <el-button class="filter-item" size="mini" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="addOrUpdateHandle(row)">
            编辑
          </el-button>
          <el-button class="filter-item" size="mini" style="margin-left: 10px;" type="primary" icon="el-icon-delete" @click="deleteHandle(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageCurrent" :limit.sync="listQuery.limit" @pagination="getList" />

    <addOrUpdateOpen v-if="addOrUpdateView" ref="addOrUpdateOpen" :userId="userId" @closeOpen="getList"></addOrUpdateOpen>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Pagination from '@/components/Pagination'
import { addOrUpdateOrDeleteInvestGroup, findGroupByPage } from '@/api/afterInvest'
import addOrUpdateOpen from './addOrUpdateOpen.vue'
import { rmoney } from '@/utils'

export default {
  name: 'AfterManageCombination',
  components: { addOrUpdateOpen, Pagination },
  props: {
    isOpen: {
      type: Boolean,
      defaultL: false
    }
  },
  data() {
    return {
      addOrUpdateView: false,
      listLoading: false,
      dateForm: {},
      total: 0,
      list: [],
      listQuery: {
        pageCurrent: 1,
        limit: 20
      },
      rmoney: Function
    }
  },
  computed: {
    ...mapGetters(['userId'])
  },
  created() {
    this.rmoney = rmoney
    this.getList()
  },
  methods: {
    groupDeltailHandle(id) {
      this.$router.push({
        path: '/afterManage/combination/details',
        query: {
          id
        }
      })
    },
    goAfterInvestHandle(row) {
      this.$router.push({
        path: '/afterManage/afterInvest',
        query: {
          groupId:　row.groupId,
          groupName:　row.groupName,
        }
      })
    },
    submitHandle(row) {
      this.$emit('selctCombination', { groupId: row.groupId, groupName: row.groupName })
    },
    addOrUpdateHandle(row) {
      this.addOrUpdateView = true
      this.$nextTick(() => {
        this.$refs.addOrUpdateOpen.init(row)
      })
    },
    deleteHandle(row) {
      this.$confirm(`确认删除分组名称【${row.groupName}】此条数据？`, '提示', {
        type: 'warning'
      }).then(_ => {
        addOrUpdateOrDeleteInvestGroup({
          groupId: row.groupId,
          groupName: row.groupName,
          deleteFlag: 0
        }).then(res => {
          if(res.code == 0) {
            this.$notify({
              message: res.msg, 
              type: 'success',
              duration: 2000
            })
            this.getList()
          }
        })
      }).catch(_ => {})
    },
    resetForm() {
      this.$refs.dateForm.resetFields()
      this.getList()
    },
    getList() {
      this.listLoading = true
      findGroupByPage({
        ...this.dateForm,
        ...this.listQuery,
        userId: this.userId
      }).then(res => {
        this.listLoading = false
        this.list = res.body.list
        this.total = res.body.total
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>

